package jetty;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Title: TabReplace4Space
 * </p>
 * <p>
 * Description: 调用方法 替换项目中所有java文件 TAB换为4个空格
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author suns suntion@yeah.net
 * @since 2017年2月20日下午2:12:37
 */
public class TabReplace4Space {

    /**
     * 文件的路径
     */
    private static final String filePath = "G:/eclipse-sts-bundle-suns/workspace-sts/suns_framework/branches/20170207_jpa_rename/suns-build";

    /**
     * 文件后缀
     */
    private static final String fileSuffix = "java";

    /**
     * 文件列表
     */
    private static List<File> filelist = new ArrayList<>();

    /**
     * 调用
     * 
     * @param args
     *            参数
     */
    public static void main(String[] args) {
        try {
            // 读取文件
            getFileList(filePath, fileSuffix);

            // 替换文件
            replaceFile();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 递归获取文件
     * 
     * @param filePath
     *            文件路径
     * @param fileSuffix
     *            文件后缀
     */
    public static void getFileList(String filePath, String fileSuffix) {
        File dir = new File(filePath);

        // 该文件目录下文件全部放入数组
        File[] files = dir.listFiles();

        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                // 判断是文件还是文件夹
                if (files[i].isDirectory()) {
                    getFileList(files[i].getAbsolutePath(), fileSuffix);
                } else if (fileName.endsWith(fileSuffix)) {
                    filelist.add(files[i]);
                } else {
                    continue;
                }
            }
        } else {
            System.out.println("目录：" + filePath + "下，未找到文件，请检查文件路径是否正确！");
        }
    }

    /**
     * 替换文件
     */
    public static void replaceFile() {
        for (File file : filelist) {
            try {
                BufferedReader input = new BufferedReader(new FileReader(file));

                String line = "";
                List<String> replaceList = new ArrayList<>();
                while ((line = input.readLine()) != null) {
                    if (line.contains("\t")) {
                        line = line.replaceAll("\t", "    ");
                    }
                    replaceList.add(line);
                }
                input.close();

                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                for (String replace : replaceList) {
                    writer.write(replace);
                    writer.newLine();
                }
                
                System.out.println("replace file : " + file.getName());
                writer.flush();
                writer.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
